<?php

/*Define SYSTEM Framework PATH*/
defined('SYSTEM_PATH') or define('SYSTEM_PATH',dirname(__FILE__));

/*Define SYSTEM Framework debug_mode*/
defined('DEBUG_MODE') or define('DEBUG_MODE',false);

define('SYSTEM_INCLUDED',true);

/* SysBase类，为全框架可公用部分，包括启动命令 */

Class SysBase{

    private static $_bootfiles = array(
									   'Config' => '/Core/Config.class.php',
									   'BaseModel' => '/Core/BaseModel.class.php',
									   'BaseController' => '/Core/BaseController.class.php',
									   'WebApp' => '/Core/App.class.php',
									   'CommonException' => '/Exception/CommonException.class.php',
									   'Session' => '/Core/Session.class.php');
	private static $_imports=array();
	private static $_instance=array();

    /* 获取版本号 */
	public static function getVersion()
	{
		return array( 'ver' => '0.0.2' , 'build' => '20090624' , 'rev' => 1);
	}
	
    /* 获取作者 */
	public static function getAuthor()
	{
		return array( 'author' => 'Horse Luke[IMIS 06, SCNU]' , 'e_mail' => 'horseluke@126.com');
	}


	/* 启动框架 */
	public static function startup(){
        set_magic_quotes_runtime(0);
		//错误信息控制显示
		if(DEBUG_MODE){
			error_reporting(E_ALL | E_STRICT);
		}else{
			error_reporting(0);
		}
		//导入启动文件准备启动
		foreach (self::$_bootfiles as $filename => $filepath){
			require (SYSTEM_PATH.$filepath);
		}
		//导入配置文件
		Config::import(require(SYSTEM_PATH.'/Config/Config.php'));
		if(is_file(APP_PATH.'/Config/Config.php')){
		    Config::import(require(APP_PATH.'/Config/Config.php'));
		}
		
	}


	/* 导入单个文件，格式为“根目录（system为系统根目录，app为应用程序根目录）.子目录x.文件名” */
	public static function import($string){
		
		if(isset(self::$_imports[$string])){     //若以前有导入，则不再重复导入
			return true;
		}
		$string_temp = $string;
		$string = explode('.',$string);
		if ( 'system' == strtolower($string[0])){
			$string[0] = SYSTEM_PATH;
		}elseif('app' == strtolower($string[0])){
			$string[0] = APP_PATH;
		}else{
			return false;
			//throw new CommonException("You did not assign Root Directory PATH (Whether Application PATH or SYSTEM Framework PATH).");
		}
		$string = implode(DIRECTORY_SEPARATOR,$string);
		if((!is_file($string.'.class.php'))){
			return false;
			//throw new CommonException("Can not find specific file!");
		}
		require ($string.'.class.php');
		self::$_imports[$string_temp] = true;
		return true;
	}
	
	/* 取得一个单例，并返回该对象 */
	public static function getInstance($className,$config = ''){
		if(class_exists($className)){
		    if(empty(self::$_instance[$className])){
			    self::$_instance[$className] = new $className($config);
		    }
		    return self::$_instance[$className];
		}else{
			return false;
		}

	}

}